package fr.poc.rest.job;

import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.netflix.appinfo.InstanceInfo;
import com.netflix.config.DynamicPropertyFactory;
import com.netflix.discovery.DiscoveryManager;

import fr.poc.domain.Couple;

import fr.poc.domain.Couple;

@Component
//@EnableScheduling
public class ScheduledJerseyAppli1Client {
	private final Logger logger = LoggerFactory.getLogger(getClass());

	private static final DynamicPropertyFactory configInstance = DynamicPropertyFactory.getInstance();

	@Value("${target.base.url}")
	private String targetBaseUrl;

	private Client client = ClientBuilder.newClient();

	@Scheduled(initialDelay = 30000, fixedDelay = 10000)
	public void callHello() {
		// String uri = targetBaseUrl + "/hello";
		
		logger.info(">>>>>>>>>>>>>callHello==========================");

		// eureka
		String vipAddress = configInstance.getStringProperty(
				"eureka.vipAddressTarget", "pocserviceA.mydomain.net").get();
		InstanceInfo nextServerInfo = DiscoveryManager.getInstance()
				.getDiscoveryClient()
				.getNextServerFromEureka(vipAddress, false);

		String uri = "http://" + nextServerInfo.getHostName() + ":"
				+ nextServerInfo.getPort() + "/rest/hello";

		logger.info(">>callHello>>>>vipAdress={} , uri={}", vipAddress, uri);

		WebTarget target = client.target(uri);
		String greetings = target.request(MediaType.APPLICATION_JSON_TYPE).get(
				String.class);

		logger.info(">>callHello>>>> URI:{} / REPONSE:{}", uri, greetings);
		logger.info("<<<<<<<<<<<<<<<<callHello==========================");

	}

	@Scheduled(initialDelay = 45000, fixedDelay = 10000)
	public void callSplit() {
		// String uri = targetBaseUrl + "/multiply";
		logger.info(">>>>>>>>>>>>>callSplit==========================");
		
		String outParam = " pierre;paul;jack";
		
		// eureka
		String vipAddress = configInstance.getStringProperty(
				"eureka.vipAddressTarget", "pocserviceB.mydomain.net").get();
		InstanceInfo nextServerInfo = DiscoveryManager.getInstance()
				.getDiscoveryClient()
				.getNextServerFromEureka(vipAddress, false);

		String uri = "http://" + nextServerInfo.getHostName() + ":"
				+ nextServerInfo.getPort() + "/rest/";

		logger.info(">>callSplit>>>>vipAdress={} , uri={}", vipAddress, uri);

		WebTarget target = client.target(uri);
		final String[] response = target.path("split").queryParam("chaine", outParam).request()
				.accept(MediaType.APPLICATION_JSON).get(String[].class);

		logger.info(">>callSplit>>>> URI:{} / REPONSE:{}", uri, response);
		logger.info("<<<<<<<<<<<<<<<<callSplit==========================");

	}

	@Scheduled(initialDelay = 40000, fixedDelay = 10000)
	public void callAdd() {
		int a = (int) (Math.random() * 100), b = (int) (Math.random() * 10);

		Couple c = new Couple(a, b);

		logger.info(">>>>>>>>>>>>>callAdd==========================");
		// String uri = targetBaseUrl + "/concat";

		// eureka
		String vipAddress = configInstance.getStringProperty(
				"eureka.vipAddressTarget", "pocserviceB.mydomain.net").get();
		InstanceInfo nextServerInfo = DiscoveryManager.getInstance()
				.getDiscoveryClient()
				.getNextServerFromEureka(vipAddress, false);

		String uri = "http://" + nextServerInfo.getHostName() + ":"
				+ nextServerInfo.getPort() + "/rest/add";

		WebTarget target = client.target(uri);
		int response = 0;
		try {
			response = target.request(MediaType.APPLICATION_JSON_TYPE)
					.accept(MediaType.APPLICATION_JSON_TYPE)
					.post(Entity.entity(c,
							MediaType.APPLICATION_JSON_TYPE), Integer.class);

		} catch (Exception e) {
			logger.error("error in callAdd ", e);
		} finally {
			logger.info(">>callAdd>>>> URI:{} / REPONSE:{}", uri, response);
			logger.info("<<<<<<<<<<<<<<<<callAdd==========================");
		}
	}

}
